function open_resetpassword(){
    $("#reset_password1").toggle(500);
}
function toogleupload(){
    $("#avatarupload-wrap").toggle(500);
}
function preupload(){
    var filename = $("#file1").get(0).files[0].name;
    filename = filename.substring(0, filename.lastIndexOf("."));
    $.getJSON("/getuploadtoken.json",{
        "input" : filename
    },function(data){
        $("#st").html("正在上传中，请稍候。。。");
        doUpload(data.token, data.key, data.upapi, data.domain);
    });
}
function doUpload(token, key, upapi, domain){
    var formData = new FormData();
    formData.append('key', key);
    formData.append('token', token);
    formData.append('file', $("#file1").get(0).files[0]);
    $.ajax({
        url: upapi,
        contentType: false,
        data: formData,
        processData: false,
        type: 'POST',
        success: function(data){
            var resourceUrl = domain + data.key;
            $("#st").html("上传成功。");
            $("#afterupload").show(500);
            $("#avatarkey").val(data.key);
            $("#lgavatar").attr('src', resourceUrl);
            $("#previewavatar").attr('src', resourceUrl);
            $("#lgavatar").Jcrop({
                onChange:showPreview,
                onSelect:showPreview,
                aspectRatio:1
            });
        },
        error: function(xhr, error, obj){
            var data = JSON.parse(xhr.responseText);
            $("#st").html("上传失败：" + data.error);
        }
    });
}
function showPreview(coords){
    if(parseInt(coords.w) > 0){
        var rx = $("#previewbox").width() / coords.w;
        var ry = $("#previewbox").height() / coords.h;
        $("#previewavatar").css({
            width:Math.round(rx * $("#lgavatar").width()) + "px",
            height:Math.round(rx * $("#lgavatar").height()) + "px",
            marginLeft:"-" + Math.round(rx * coords.x) + "px",
            marginTop:"-" + Math.round(ry * coords.y) + "px"
        });
        $("#cropx").val(coords.x);
        $("#cropy").val(coords.y);
        $("#cropw").val(coords.w);
        $("#croph").val(coords.h);
    }
}
function saveavatar(){
    var x=$("#cropx").val();
    var y=$("#cropy").val();
    var w=$("#cropw").val();
    var key=$("#avatarkey").val();
    $.getJSON("/cropavatartoken.json",{
        "key" : key,
        "x" : x,
        "y" : y,
        "w" : w
    },function(data){
        if(data.result = "success") {
            var queryurl = data.queryurl;
            var avatarurl = data.avatarurl;
            $.get(queryurl, {}, function (data) {
                $("#newavatar").val(avatarurl);
                newavatar();
            });
        }else{
            alert(data.info);
        }
    });
}
function newavatar(){
    var newavatar=$("#newavatar").val();
    if(!newavatar){
        alert("图片地址获取失败。");
        return;
    }
    $.getJSON("/user/setavatar.json",{
        "newavatar" : newavatar
    }, function(data){
        if(data.result=="error"){
            alert(data.info);
        }else{
            alert("头像修改成功。");
            $("#ssavater").attr('src', newavatar);
            $("#afterupload").hide(500);
            $("#avatarupload-wrap").hide(500);
        }
    });
}
function resetpassword(){
    $("#rep-m").hide(500);
    $("#repass-message").removeClass("alert-success").addClass("alert-danger")
    var pass1 = $("#password").val();
    var pass2 = $("#newpassword").val();
    var pass3 = $("#renewpassword").val();
    if(pass3!=pass2){
        $("#newpassword").val("").focus();
        $("#renewpassword").val("");
        $("#repass-message").html("两次密码不一致。");
        $("#rep-m").show(500);
    }else
    if(pass1==""){
        $("#password").val("").focus();
        $("#repass-message").html("密码不能为空。");
        $("#rep-m").show(500);
    }else
    if(pass2==""){
        $("#newpassword").val("").focus();
        $("#repass-message").html("密码不能为空。");
        $("#rep-m").show(500);
    }else
        $.post("/user/resetpassword.json", {
            "password": pass1,
            "newPassword" : pass2
        }, function(data){
            data = JSON.parse(data);
            if(data.result=="error"){
                $("#repass-message").html("密码修改失败："+data.info);
                $("#rep-m").show(500);
            }else{
                $("#repass-message").removeClass("alert-danger").addClass("alert-success").html("修改成功。");
                $("#rep-m").show(500);
            }
        });
}